<template>
  <BasicModal v-bind="$attrs" @register="register" showFooter :title="getTitle" width="50%" @ok="handleSubmit" :okButtonProps="{loading}">
    <BasicForm @register="registerForm">

    </BasicForm>
  </BasicModal>
</template>
<script lang="ts" setup>
import {ref, computed, unref, type PropType} from 'vue';
import { BasicForm, useForm } from '@/components/Form';
import { formSchema } from './role.data';
import { BasicModal, useModalInner } from '@/components/Modal';
import { dictSaveApi } from '@/api/sys/msUser';

import { useMessage } from "@/hooks/web/useMessage";

import { useUserStore } from "@/store/modules/user";
import type {Recordable} from "@vben/types";
const userStore = useUserStore();
const props = defineProps({
  approveNo: { type: String,default:"" },
  passApi: { type: Function as PropType<(arg?: any) => Promise<Recordable<any>>> },
  backApi: { type: Function as PropType<(arg?: any) => Promise<Recordable<any>>> },
}  );
const emit = defineEmits(['success']);
const isUpdate = ref(true);
const { createMessage } = useMessage();

const id = ref("");
const title = ref("");

const [registerForm, { resetFields, validate }] = useForm({
  labelWidth: 90,
  baseColProps: { span: 24 },
  schemas: formSchema,
  showActionButtonGroup: false,
});
const type=ref("");
const [register, { setModalProps, closeModal }] = useModalInner(async (data) => {
  //打开弹层后的回调
  resetFields();
  setModalProps({ confirmLoading: false });
  type.value=data.type;
  title.value=`审批${data.type}`;
  // isUpdate.value = !!data?.isUpdate;
  // id.value = "";
  // if (unref(isUpdate)) {
  //
  //   // console.log("---data.record:",data.record)
  //   id.value = data.record.id;
  //   let json = {
  //     id: id.value
  //   };
  //   setFieldsValue(data.record);
  // } else {
  //   //新增
  //   userStore.setChatIsUpdate(false);
  //
  // }
});

const loading=ref(false);

const getTitle = computed(() => title.value);

async function handleSubmit() {
  // try {

    const values = await validate();

  let {backApi,passApi,approveNo } = props;
  let t_json=values;
  t_json.approveNo=approveNo;
  let res;
  if(type.value=="通过"){
    loading.value = true;
    res =await passApi(t_json);
  }else if(type.value==="驳回"){
    loading.value = true;
    res =await backApi(t_json);
  }
  loading.value = false;
  if(res.code==200){
    createMessage.success(res.message);
  }else{
    createMessage.error(res.message);
  }
  closeModal();
  emit("success");

}




</script>
